﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Orgs;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.Orgs.Pages
{
    public partial class OrgTree : SsTemplatePage
    {
        private Guid _selNodeId;

        protected void Page_Load(object sender, EventArgs e)
        {
            _selNodeId = Request.GetGuid("nodeId");
            this.btn_AddNode.Enabled = true;
            this.btn_AddNode.NavigateUrl = string.Format("Node-Add.aspx?nodeID={0}", _selNodeId);
            if (_selNodeId != Guid.Empty)
            {
                this.btn_EmployeeList.NavigateUrl = string.Format("Org-Employee-List.aspx?nodeID={0}", _selNodeId);
                this.btn_EditNode.NavigateUrl = string.Format("Node-Edit.aspx?nodeID={0}", _selNodeId);
                //this.btn_DelNode.SupportsDisabledAttribute
                this.btn_DelNode.Enabled = true;
            }
            else
            {
                this.btn_EmployeeList.Enabled = false;
                this.btn_EditNode.Enabled = false;
                this.btn_DelNode.Enabled = false;
            }

            if (!IsPostBack)
            {
                OrgsService service = new OrgsService();
                List<Node> list = service.GetChildren(Guid.Empty, true);
                foreach (var menu in list)
                {
                    var tNode = new TreeNode(menu.Name, menu.Id.ToString());
                    this.Org_Tree.Nodes.Add(tNode);
                    if (menu.Id == _selNodeId)
                    {
                        tNode.Selected = true;
                        BindOrgDetail(menu);
                    }
                    tNode.NavigateUrl = "OrgTree.aspx?nodeId=" + menu.Id;
                    LoadTree(tNode, menu);
                }
            }
        }

        private void LoadTree(TreeNode node, Node orgNode)
        {
            foreach (var menu in orgNode.Children)
            {
                var tNode = new TreeNode(menu.Name, menu.Id.ToString());
                tNode.NavigateUrl = "OrgTree.aspx?nodeId=" + menu.Id;
                node.ChildNodes.Add(tNode);
                if (menu.Id == _selNodeId)
                {
                    tNode.Selected = true;
                    BindOrgDetail(menu);
                }
                LoadTree(tNode, menu);
            }
        }

        protected void btn_DelNode_Click(object sender, EventArgs e)
        {
            OrgsService service = new OrgsService();
            String nodeID = Request.QueryString["nodeId"] ?? string.Empty;
            if (nodeID == "")
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Delete", "alert('请选择一个节点删除!');", true);
                return;
            }
            bool bl = service.DeleteNode(service.GetNode(new Guid(nodeID), true));
            if (bl)
            {
                Response.Redirect("OrgTree.aspx");
            }
        }

        private void BindOrgDetail(Node orgNode)
        {
            _ctlEditCode.Text = orgNode.Code;
            _ctlEditName.Text = orgNode.Name;
        }
    }
}